<template>
	<view>
		<image src="http://imcdn.shimaruanchuang.cn/chat/1708262586186KLofoUAZi9Veea8abda383092fd2087a08dbea0aec4d.jpg" style=" height: 250rpx; margin-top: 30%;width: 70%; margin-left: 17%;"></image>
		<view style="margin-top: 10%;width: 70%; margin-left: 15%;color: #fff;">
			<u--input color="#acacac" placeholder="手机号码:" border="surround" v-model="tel" shape="circle"  ></u--input>
				
		</view>

		<view style="margin-top: 5%;width: 70%; margin-left: 15%; ">
			<u--input color="#acacac" placeholder="输入密码:" border="surround" v-model="passWord" shape="circle"  ></u--input>
		</view>


		<view style=" margin-left: 40%; ">
			<view style="margin-top: 5%;">
				<u-row customStyle="margin-bottom: 10px">
					<u-col span="3">
						<view @click="registerFun">
							<u--text type="primary" text="注册"></u--text>
						</view>
					</u-col>
					<u-col span="3">
						<view @click="loginFun">
							<u--text type="primary" text="登录"></u--text>
						</view>

					</u-col>
				</u-row>
			</view>
		</view>

		<view class="weixin" style="margin-left: 45%;margin-top: 40%;">
			<u-icon name="weixin-fill" size="50" color="#32a33b"  @click="onWeixin"></u-icon>
			<view class="weixinText" style="font-size: 23rpx; color: #acacac;" @click="onWeixin">
				一键登录
			</view>
		</view>
	</view>
</template>

<script>
	import {
		register,
		login,
		hideMessageList,
		oneClickLogin
	} from "../../config/api.js"
	export default {
		data() {
			return {
				value: '',
				tel: null,
				passWord: null,
				openId : "",
			}
		},
		onShow() {
			this.getDemo()
			this.uniloginFun();
		},
		methods: {
			onWeixin(){
				uni.getUserProfile({
					desc:"登陆后同步数据",
					lang: "zh_CN",
					success(ures){
					console.log(ures);	
					uni.login({
						success: (weixinResult) => {
							//这里返回的不是openId   是code  这个code要给服务器后端   后端解析  稍等我找个代码 
							//
							console.log("微信给的code:",weixinResult.code);
							//同时拿到微信给的code以后，给param的json再加上微信给的code  然后把param一下全给后端  这样既有用户输入的账号密码  也右微信给的code
							let param = {
								code : weixinResult.code
							}							// 发出post，假设需要带上token auth=true  代表该请求会默认带上登录的token
							oneClickLogin(param, { custom: { auth: false } }, ).then((e) => {
								console.log(e);
								if (e.code == 200) {
									uni.showToast({
										title: '登录成功',
										icon: "success"
									})
									console.log(e)
									uni.setStorageSync("userInfo", e.data);
									uni.reLaunch({
										url:"/pages/index/index"
									})
								}
							}).catch((e) => {

								console.log("请求失败了",e)
							})
						}
					})

					},
					fail(e){
						console.log(e);
					}
				})
			},
			getDemo() {
				return
				//往后端传的参数
				let param = {
					tel: this.tel,
					passWord: this.passWord,
				}
				hideMessageList({
					custom: {
						auth: true,
						loading: false
					},
					params: param
				}).then((e) => {
					console.log(e);
				}).catch(() => {

				})

			},
			loginFun() {
				let that = this 
				//往后端传的参数  得先判断手机号或者密码是否输入符合格式   
				let param = {
					tel: that.tel,
					passWord: that.passWord 
				}
				if (param.tel == null || param.tel == "" || param.tel.length != 11) {
					uni.showToast({
						title: '手机号输入有误',
						icon: "error"
					})
					return;
				}
				if (param.passWord == null || param.passWord == "") {
					uni.showToast({
						title: '密码输入有误',
						icon: "error"
					})
					return;
				} // 
				//当用户点击登陆按钮的时候   先微信登陆，获取到code    再发送登陆请求给后端，  同时带上code
				////上边手机号密码输入的格式正确后再登陆
				uni.login({
					success: (weixinResult) => {
						//这里返回的不是openId   是code  这个code要给服务器后端   后端解析  稍等我找个代码 
						//
						console.log("微信给的code:",weixinResult.code);
						//同时拿到微信给的code以后，给param的json再加上微信给的code  然后把param一下全给后端  这样既有用户输入的账号密码  也右微信给的code
						param.code=weixinResult.code,
						// 发出post，假设需要带上token auth=true  代表该请求会默认带上登录的token
						login(param, { custom: { auth: false } }, ).then((e) => {
							if (e.code == 200) {
								uni.showToast({
									title: '登录成功',
									icon: "success"
								})
								console.log(e)
								uni.setStorageSync("userInfo", e.data);
								uni.reLaunch({
									url:"/pages/index/index"
								})
							}
						}).catch(() => {
							console.log("请求失败了")
						})
					}
				})
			
			
			},
			uniloginFun(){
				let that = this
			
			},
			registerFun() {
				uni.navigateTo({
					url: "/pages/register/register"
				})
				

			}
		}
	}
</script>

<style>
	page {
		background: #1a1a1a;
	}
</style>